/**
 * Created by MBENBEN on 2017/5/22.
 */
$('.bold').on('click', function() {
	document.execCommand("Bold", true, null);
	// document.execCommand(foreColor,0,'#7e2412');
	console.log('clllldfsdf');
})

function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //
	var r = window.location.search.substr(1).match(reg); //
	if(r != null) return unescape(r[2]);
	return null; //
}
var nowId;
var httpUrl = '/';

//var httpUrl = 'http://127.0.0.1:3000/';
function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}

if(!getCookie('login_name')) {
	window.location.href = 'account.html';
}
var stauts = '0';
if(getUrlParam('stauts') && getUrlParam('stauts') == '2') {
	$('.tab-h span').eq(0).removeClass('tab-active')
	$('.tab-h span').eq(1).addClass('tab-active')
	stauts = getUrlParam('stauts')
}
$(function() {
	getList()
	$('.close').on('click', function() {
		$('.m-edit').fadeOut();
	})

	$(document).on('click', '.bj-btn', function() {
		var $this = $(this).parent().parent();
		console.log($this)
		$('.m-edit').fadeIn();
		$('.e-title input').val($this.data('title'))
		nowId = parseInt($this.data('id'))
		getFid(parseInt($this.data('id')))
	})
	var $id;
	$(document).on('click', '.item-close', function() {
		$id = $(this).parent().data('id');
		$('.hmbPop').fadeIn(500);
		$('.hmbBack').fadeIn(500);
	})
	$('.extend-close').on('click', function() {
		$('.hmbPop').fadeOut(500)
		$('.hmbBack').fadeOut(500)
	})
	$(' .confirm-btn').on('click', function() {
		$('.hmbPop').fadeOut(500)
		$('.hmbBack').fadeOut(500)
		remove($id)
	})
	$('.add-btn').on('click', function() {
		addNew()
		$('.e-title input').val('')

		$('.body-html').summernote({
			height: 350,
			width: 750,
			tabsize: 2
		});
		$('.body-html').summernote('code', '')
		$('.author-input').val('道然');
		$('.gname-input').val('归灵健康');
		$('.gurl-input').val('https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=MzUyOTAzNTQzMw==&scene=124#wechat_redirect')
		$('.m-edit').fadeIn();
	})

	$('.tab-h span').on('click', function() {
		$('.tab-h span').removeClass('tab-active')
		$(this).addClass('tab-active')
		if($('.tab-h span').index($(this)) == 0) {
			window.location.href = 'index.html?stauts=0'

		} else {
			window.location.href = 'index.html?stauts=2'
		}
	})

	$('.m-save').on('click', function() {
		var isNew = '0'
		if($('.pub-check').is(':checked')) {
			isNew = '2'
		} else {
			isNew = '0'
		}
		update({
			id: nowId,
			title: $('.e-title input').val(),
			content: $('.body-html').summernote('code'),
			stauts: isNew,
			author: $('.author-input').val(),
			g_name: $('.gname-input').val(),
			g_url: $('.gurl-input').val(),
			review: parseInt($('.review-input').val()),
			power: parseInt($('.power-input').val())
		})
	})
	$('.tao-btn').on('click', function() {
		//$('.tao-input').val();
		var taoHtml = '<div class="tao-Div">' +
			'<span class="copy-all-text">长按框内->全选->复制</span>' +
			'<p class="copy-text text j_TokenContent">复制框内整段文字' + $('.tao-input').val() + '，打开「手淘」即可「领取优惠券」并购买</p>' +
			'<div class="bar">' +
			'<a class="j_CopyToken btn tpwd" data-clipboard-text="复制框内整段文字' + $('.tao-input').val() + '，打开「手淘」即可「领取优惠券」并购买">一键复制</a>' +
			'</div>' +
			'</div>'
		$('.body-html').summernote('code', $('.body-html').summernote('code') + taoHtml)
		$('.tao-input').val('');
	})
	//        $('.summernote').summernote({
	//            height: 400,
	//            tabsize: 2
	//        });
	//        $('.summernote').summernote('code','<div>内容编辑</div>')
})

function remove(id) {
	$.ajax({
		url: httpUrl + 'users/remove',
		type: 'post',
		data: {
			id: id
		},
		success: function(result) {
			if(result.success) {
				window.location.href = ''
			} else {

			}
		}
	})
}

function update(data) {
	$.ajax({
		url: httpUrl + 'users/updateInfo',
		type: 'post',
		data: data,
		success: function(result) {
			if(result.success) {
				$('.m-edit').fadeOut();
				window.location.href = ''
			} else {

			}
		}
	})
}

function addNew() {
	$.ajax({
		url: httpUrl + 'users/add',
		type: 'post',
		success: function(result) {
			console.log(result.success)
			if(result.success) {
				nowId = result.id;

			} else {

			}
		}
	})
}

function getFid(id) {
	$.ajax({
		url: httpUrl + 'users/getInfo',
		type: 'get',
		data: {
			id: id
		},
		success: function(result) {
			if(result) {
				// console.log(result)
				var newData = result.data[0]
				console.log(newData.stauts)
				if(newData.stauts == '2') {
					$('.pub-check').prop("checked", true)
				} else {
					$('.pub-check').prop("checked", false)
				}
				if(newData.author && newData.author != '') {
					$('.author-input').val(newData.author);
					$('.gname-input').val(newData.g_name);
					$('.gurl-input').val(newData.g_url)
					$('.review-input').val(newData.review)
					$('.power-input').val(newData.power)
				}
				//$('.body-html').html(result.data[0].content)
				$('.body-html').summernote({
					height: 350,
					width: 750,
					tabsize: 2
				});
				$('.body-html').summernote('code', result.data[0].content)
			}
		}
	})
}

function getList() {
	$.ajax({
		url: httpUrl + 'users/getList?stauts=' + stauts,
		type: 'get',
		success: function(result) {
			if(result) {
				// console.log(result)
				var i;
				var newData = result.data
				for(i = 0; i < newData.length; i++) {
					if (stauts=='0') {
						$('.m-body').append('<div class="m-item af" data-id="' + newData[i].id + '" data-title="' + newData[i].title + '"><div class="item-close">×</div> <p>' + newData[i].title + '</p> <div> <span class="m-btn bj-btn" style="margin-bottom:5px">编辑</span> <a class="m-btn" href="motion.html?id=' + newData[i].id + '">预览</a> </div> </div>')
					}else{
						$('.m-body').append('<div class="m-item af" data-id="' + newData[i].id + '" data-title="' + newData[i].title + '"><div class="item-close">×</div> <p>' + newData[i].title + '</p> <div> <span class="m-btn bj-btn" style="margin-bottom:5px">编辑</span> <a class="m-btn" href="html/ac'+parseInt(newData[i].id)+'.html">预览</a> </div> </div>')
					}
					
				}
			}
		}
	})
}